package cn.wolfcode.wolf2w.job;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.temporal.Temporal;
import java.util.List;

/**
 * redis数据持久化
 */
@Component
public class RedisDataPersistenceJob {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0 0 0 1/1 * ? 
    @Scheduled(cron="0 0 0,6,12,18 * * ?")
    //@Scheduled(cron="0/10 * * * * ?")
    public void redisDataPersistence(){
        //需要持久化的数据,攻略统计对象vo,用户收藏攻略
        //根据路径查找攻略统计对象集合
        List<StrategyStatisVO> vos = strategyStatisVORedisService.queryStrategyStatisVos();
        //遍历集合将vos对象更新到mongodb中
        for (StrategyStatisVO vo : vos) {
            strategyService.updateStatisVo(vo);
        }

        //根据路径查找用户列表
        List<UserInfo> userInfos = userInfoRedisService.queryUserFavorStrategyList();
        for (UserInfo userInfo : userInfos) {
            userInfoService.update(userInfo);
        }
    }
}